import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/base_router_config.dart';
import 'package:wallet_utils/report_widget/base/callback/base_callback.dart';
import 'package:wallet_utils/toast.dart';

import '../../mine/api/mine_api.dart';
import '../../mine/bean/aboutUsBean.dart';
import '../../mine/contentType.dart';
import '../api/home_api.dart';
import '../bean/userAgreementBean.dart';
import 'state.dart';

class OfflineProductLogic extends GetxController {
  final OfflineProductState state = OfflineProductState();
  num? face;
  num? isShowIdCard;
  bool showContacts = false;
  num? channelAuthType;
  ProductInfo? productInfo;
  bool isShowIncoming = false;

  @override
  void onInit() {
    super.onInit();
    if (Get.arguments != null) {
      productInfo = Get.arguments['productInfo'];
      face = Get.arguments['face'];
      showContacts = Get.arguments['showContacts'];
      isShowIdCard = Get.arguments['isShowIdCard'];
      channelAuthType = Get.arguments['channelAuthType'];
      if (productInfo!.companyName!.isNotEmpty) {
        isShowIncoming = true;
      }
    }
  }

  //信用授权书
  getUserInfo(int type) {
    MineApi.getHTML(
        type,
        BaseCallback(onSuccess: (map) {
          AboutUsBean aboutUsBean = AboutUsBean.fromJson(map);
          if (aboutUsBean.data != null) {
            state.name = aboutUsBean.data!.name!;
            state.content = aboutUsBean.data!.content!;
            update();
          }
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            ToastUtils.showToast(msg ?? "");
          });
        }));
  }

  jumpPage() async {
    if (isShowIdCard == 0) {
      await preposition();
    } else {
      if (channelAuthType == 2) {
        Get.toNamed(BaseRouterConfig.identityCertification, arguments: {
          'isShowIncoming':isShowIncoming,
          'face': face,
          'showContacts': showContacts,
          'channelAuthType': channelAuthType,
        });
      } else {
        await preposition();
      }
    }
  }

  Future<void> preposition() async {
    if (showContacts) {
      Get.toNamed(BaseRouterConfig.contactPerson, arguments: {
        'isShowIncoming': isShowIncoming,
        'face': face,
        'isShowIdCard': isShowIdCard,
        'channelAuthType': channelAuthType,
      });
    } else if (face == 1 && !showContacts) {
      await jumpFace();
    } else if (face == 0 && !showContacts) {
      Get.toNamed(BaseRouterConfig.result);
    }
  }

  Future<void> jumpFace() async {
    WidgetsFlutterBinding.ensureInitialized();
    state.cameras = await availableCameras();
    /// 获取设备摄像头，默认0:后置 1:前置
    state.firstCamera = state.cameras!.first;
    Get.toNamed(BaseRouterConfig.faceRecognition, arguments: {
      'isShowIncoming': isShowIncoming,
      'camera': state.cameras,
    });
  }
}
